/**
 * 
 */
package network;

import io.ImportGame;

import java.io.IOException;
import java.util.ArrayList;

import game.Building;
import game.BuildingType;
import game.Game;
import game.Map;
import game.Player;
import game.Unit;
import game.UnitType;

/**
 * @author jb
 *
 */
public abstract class GameNetwork extends Game {

	/**
	 * @param name
	 * @param map
	 * @param players
	 */
	public GameNetwork(String name, Map map, ArrayList<Player> players) {
		super(name, map, players);
		// TODO Auto-generated constructor stub
	}
	
	public abstract void transfertGameFile() throws IOException;
	
	public void parseCommand(String command) throws IOException
	{
		String separator = "\t";

		String[] parsedCommand = command.split(separator);

		// Initiate transfert of game file
		if(parsedCommand[0].equals("transfertGameFile")) {

			this.transfertGameFile();
		}
		
		// Change tile
		else if (parsedCommand[0].equals("setTile"))
		{
			// Update tile in tilemap
			this.setTile(true, Integer.parseInt(parsedCommand[1]), Integer.parseInt(parsedCommand[2]), Integer.parseInt(parsedCommand[3]));
		}
		
		// Place a new unit
		else if (parsedCommand[0].equals("placeUnit"))
		{
			UnitType unitType = this.getUnitTypes().get(parsedCommand[1]);
			Unit temp = new Unit(unitType,
						new Player(Integer.parseInt(parsedCommand[2]), parsedCommand[1]),
						Integer.parseInt(parsedCommand[3]),
						Integer.parseInt(parsedCommand[4]));
			temp.setHealth(Integer.parseInt(parsedCommand[5]));
			temp.setActive(Boolean.valueOf(parsedCommand[6]));
			
			//temp.setFuel(Integer.parseInt(parsedCommand[7]));
			//temp.getType().getPrimaryWeaponType().setAmmo(Integer.parseInt(parsedCommand[8]));
			
			/* Units_carried here ! */
			
			this.placeUnit(true, temp);
		}
		
		// Remove a unit
		else if (parsedCommand[0].equals("removeUnit"))
		{
			Unit temp = this.getMap().getUnit(Integer.parseInt(parsedCommand[1]), Integer.parseInt(parsedCommand[2]));
			this.removeUnit(true, temp);
		}
		
		// Place a building
		else if (parsedCommand[0].equals("placeBuilding"))
		{
			BuildingType buildingType = this.getBuildingTypes().get(parsedCommand[1]);
			Building temp = new Building(buildingType,
										 Integer.parseInt(parsedCommand[3]),
										 Integer.parseInt(parsedCommand[4]));
			temp.setPlayer(this.getPlayers().get(Integer.parseInt(parsedCommand[2])));
			
			this.placeBuilding(true, temp);
		}
		
		// Remove a building
		else if (parsedCommand[0].equals("removeBuilding"))
		{
			Building temp = this.getMap().getBuilding(Integer.parseInt(parsedCommand[1]), Integer.parseInt(parsedCommand[2]));			
			this.removeBuilding(true, temp);
		}

		// Player has lost
		else if (parsedCommand[0].equals("playerLost"))
		{
			this.playerLost(true, this.getPlayers().get(Integer.parseInt(parsedCommand[1])));
		}
		
		// Next turn
		else if (parsedCommand[0].equals("endTurn"))
		{
			this.endTurn(true);
		}
		
		// Game over
		else if (parsedCommand[0].equals("gameOver"))
		{
			this.gameOver(true);
		}
		
	}

}
